const { v4: uuidv4 } = require('uuid');

module.exports = {
  up: async (queryInterface) => {
    const now = new Date();

    // 删除旧数据
    await queryInterface.bulkDelete('menus', null, {});

    // 系统菜单（tenant_id 为 null）
    const systemDirId = uuidv4();
    const tenantDirId = uuidv4();

    // 一级目录
    const directories = [
      {
        id: systemDirId,
        tenant_id: null,
        parent_id: null,
        name: 'system',
        title: '系统管理',
        english: 'System',
        type: 'directory',
        path: '/system',
        component: null,
        icon: 'solar:settings-linear',
        permission: null,
        order_num: 100,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: tenantDirId,
        tenant_id: null,
        parent_id: null,
        name: 'tenant',
        title: '租户管理',
        english: 'Tenant',
        type: 'directory',
        path: '/tenant',
        component: null,
        icon: 'solar:users-group-rounded-linear',
        permission: null,
        order_num: 200,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
    ];

    await queryInterface.bulkInsert('menus', directories);

    // 二级菜单
    const userMenuId = uuidv4();
    const roleMenuId = uuidv4();
    const permissionMenuId = uuidv4();
    const menuMenuId = uuidv4();
    const auditLogMenuId = uuidv4();
    const dictionaryMenuId = uuidv4();
    const tenantListMenuId = uuidv4();
    const planMenuId = uuidv4();

    const menus = [
      // === 系统管理子菜单 ===
      {
        id: userMenuId,
        tenant_id: null,
        parent_id: systemDirId,
        name: 'user',
        title: '用户管理',
        english: 'User Management',
        type: 'menu',
        path: '/system/user',
        component: 'User/index',
        icon: 'solar:user-linear',
        permission: 'user:read',
        order_num: 1,
        visible: true,
        status: true,
        cache: true,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: roleMenuId,
        tenant_id: null,
        parent_id: systemDirId,
        name: 'role',
        title: '角色管理',
        english: 'Role Management',
        type: 'menu',
        path: '/system/role',
        component: 'Role/index',
        icon: 'solar:shield-user-linear',
        permission: 'role:read',
        order_num: 2,
        visible: true,
        status: true,
        cache: true,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: permissionMenuId,
        tenant_id: null,
        parent_id: systemDirId,
        name: 'permission',
        title: '权限管理',
        english: 'Permission Management',
        type: 'menu',
        path: '/system/permission',
        component: 'Permission/index',
        icon: 'solar:lock-linear',
        permission: 'permission:read',
        order_num: 3,
        visible: true,
        status: true,
        cache: true,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: menuMenuId,
        tenant_id: null,
        parent_id: systemDirId,
        name: 'menu',
        title: '菜单管理',
        english: 'Menu Management',
        type: 'menu',
        path: '/system/menu',
        component: 'Menu/index',
        icon: 'solar:menu-dots-linear',
        permission: 'menu:read',
        order_num: 4,
        visible: true,
        status: true,
        cache: true,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: auditLogMenuId,
        tenant_id: null,
        parent_id: systemDirId,
        name: 'audit-log',
        title: '审计日志',
        english: 'Audit Log',
        type: 'menu',
        path: '/system/audit-log',
        component: 'AuditLog/index',
        icon: 'solar:file-text-linear',
        permission: 'audit_log:read',
        order_num: 5,
        visible: true,
        status: true,
        cache: true,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: dictionaryMenuId,
        tenant_id: null,
        parent_id: systemDirId,
        name: 'dictionary',
        title: '字典管理',
        english: 'Dictionary Management',
        type: 'menu',
        path: '/system/dictionary',
        component: 'Dictionary/index',
        icon: 'solar:document-linear',
        permission: 'dictionary:read',
        order_num: 6,
        visible: true,
        status: true,
        cache: true,
        redirect: null,
        created_at: now,
        updated_at: now,
      },

      // === 租户管理子菜单 ===
      {
        id: tenantListMenuId,
        tenant_id: null,
        parent_id: tenantDirId,
        name: 'tenant-list',
        title: '租户列表',
        english: 'Tenant List',
        type: 'menu',
        path: '/tenant/list',
        component: 'Tenant/index',
        icon: 'solar:user-linear',
        permission: 'tenant:read',
        order_num: 1,
        visible: true,
        status: true,
        cache: true,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: planMenuId,
        tenant_id: null,
        parent_id: tenantDirId,
        name: 'plan',
        title: '套餐管理',
        english: 'Plan Management',
        type: 'menu',
        path: '/tenant/plan',
        component: 'Plan/index',
        icon: 'solar:star-linear',
        permission: 'plan:read',
        order_num: 2,
        visible: true,
        status: true,
        cache: true,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
    ];

    await queryInterface.bulkInsert('menus', menus);

    // 三级按钮（挂在对应的菜单下）
    const buttons = [
      // === 用户管理按钮 ===
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: userMenuId,
        name: 'user:create',
        title: '添加用户',
        english: 'Create User',
        type: 'button',
        path: null,
        component: null,
        icon: 'solar:add-circle-linear',
        permission: 'user:create',
        order_num: 1,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: userMenuId,
        name: 'user:update',
        title: '编辑用户',
        english: 'Edit User',
        type: 'button',
        path: null,
        component: null,
        icon: 'solar:pen-linear',
        permission: 'user:update',
        order_num: 2,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: userMenuId,
        name: 'user:delete',
        title: '删除用户',
        english: 'Delete User',
        type: 'button',
        path: null,
        component: null,
        icon: 'solar:trash-bin-minimalistic-linear',
        permission: 'user:delete',
        order_num: 3,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },

      // === 角色管理按钮 ===
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: roleMenuId,
        name: 'role:create',
        title: '添加角色',
        english: 'Create Role',
        type: 'button',
        path: null,
        component: null,
        icon: 'solar:add-circle-linear',
        permission: 'role:create',
        order_num: 1,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: roleMenuId,
        name: 'role:update',
        title: '编辑角色',
        english: 'Edit Role',
        type: 'button',
        path: null,
        component: null,
        icon: 'solar:pen-linear',
        permission: 'role:update',
        order_num: 2,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: roleMenuId,
        name: 'role:delete',
        title: '删除角色',
        english: 'Delete Role',
        type: 'button',
        path: null,
        component: null,
        icon: 'solar:trash-bin-minimalistic-linear',
        permission: 'role:delete',
        order_num: 3,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: roleMenuId,
        name: 'role:assign-permission',
        title: '分配权限',
        english: 'Assign Permission',
        type: 'button',
        path: null,
        component: null,
        icon: 'solar:key-linear',
        permission: 'role:manage',
        order_num: 4,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },

      // === 权限管理按钮 ===
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: permissionMenuId,
        name: 'permission:create',
        title: '添加权限',
        english: 'Create Permission',
        type: 'button',
        path: null,
        component: null,
        icon: 'solar:add-circle-linear',
        permission: 'permission:create',
        order_num: 1,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: permissionMenuId,
        name: 'permission:update',
        title: '编辑权限',
        english: 'Edit Permission',
        type: 'button',
        path: null,
        component: null,
        icon: 'solar:pen-linear',
        permission: 'permission:update',
        order_num: 2,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: permissionMenuId,
        name: 'permission:delete',
        title: '删除权限',
        english: 'Delete Permission',
        type: 'button',
        path: null,
        component: null,
        icon: 'solar:trash-bin-minimalistic-linear',
        permission: 'permission:delete',
        order_num: 3,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },

      // === 菜单管理按钮 ===
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: menuMenuId,
        name: 'menu:create',
        title: '添加菜单',
        english: 'Create Menu',
        type: 'button',
        path: null,
        component: null,
        icon: 'solar:add-circle-linear',
        permission: 'menu:create',
        order_num: 1,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: menuMenuId,
        name: 'menu:update',
        title: '编辑菜单',
        english: 'Edit Menu',
        type: 'button',
        path: null,
        component: null,
        icon: 'solar:pen-linear',
        permission: 'menu:update',
        order_num: 2,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: menuMenuId,
        name: 'menu:delete',
        title: '删除菜单',
        english: 'Delete Menu',
        type: 'button',
        path: null,
        component: null,
        icon: 'solar:trash-bin-minimalistic-linear',
        permission: 'menu:delete',
        order_num: 3,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },

      // === 租户管理按钮 ===
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: tenantListMenuId,
        name: 'tenant:create',
        title: '添加租户',
        english: 'Create Tenant',
        type: 'button',
        path: null,
        component: null,
        icon: 'solar:add-circle-linear',
        permission: 'tenant:create',
        order_num: 1,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: tenantListMenuId,
        name: 'tenant:update',
        title: '编辑租户',
        english: 'Edit Tenant',
        type: 'button',
        path: null,
        component: null,
        icon: 'solar:pen-linear',
        permission: 'tenant:update',
        order_num: 2,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: tenantListMenuId,
        name: 'tenant:delete',
        title: '删除租户',
        english: 'Delete Tenant',
        type: 'button',
        path: null,
        component: null,
        icon: 'solar:trash-bin-minimalistic-linear',
        permission: 'tenant:delete',
        order_num: 3,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },

      // === 套餐管理按钮 ===
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: planMenuId,
        name: 'plan:create',
        title: '添加套餐',
        english: 'Create Plan',
        type: 'button',
        path: null,
        component: null,
        icon: 'solar:add-circle-linear',
        permission: 'plan:create',
        order_num: 1,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: planMenuId,
        name: 'plan:update',
        title: '编辑套餐',
        english: 'Edit Plan',
        type: 'button',
        path: null,
        component: null,
        icon: 'solar:pen-linear',
        permission: 'plan:update',
        order_num: 2,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: planMenuId,
        name: 'plan:delete',
        title: '删除套餐',
        english: 'Delete Plan',
        type: 'button',
        path: null,
        component: null,
        icon: 'solar:trash-bin-minimalistic-linear',
        permission: 'plan:delete',
        order_num: 3,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
    ];

    await queryInterface.bulkInsert('menus', buttons);
  },

  down: async (queryInterface) => {
    await queryInterface.bulkDelete('menus', null, {});
  },
};

